//
//  EpointContactsDetailsViewController.m
//  EpointFrame5
//
//  Created by 李林 on 14/6/7.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "EpointContactsDetailsViewController.h"
#import "JYTextField.h"
#import "SGActionView.h"
#import "MailInfo.h"
#import "EpointMailSendViewController.h"
#import "UIAlertView+Blocks.h"
@interface EpointContactsDetailsViewController ()

@end

@implementation EpointContactsDetailsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if (CurrentDeviceISiPad) {
            self = [super initWithNibName:@"EpointContactsDetailsViewController_pad" bundle:nil];
        }else{
            if(IS_IPHONE_5){
                self = [super initWithNibName:@"EpointContactsDetailsViewController_5" bundle:nil];
            }else{
                self = [super initWithNibName:@"EpointContactsDetailsViewController" bundle:nil];
            }
        }
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:UserGuid forKey:@"userguid"];
        [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    UserGuid = [[NSString alloc]initWithString:[initParam objectForKey:@"userguid"]];
     userDetail=[initParam objectForKey:@"userDetail"];
    if (userDetail==nil) {
        userDetail = [DBFrameService getUserDetailByUserguid:UserGuid];
    }
    
    
    self.title =[userDetail objectForKey:@"displayname"];
    [self.nameLabel setText:[userDetail objectForKey:@"displayname"]];
    [self.sexLabel setText:[userDetail objectForKey:@"sex"]];
    [self.deptLabel setText:[userDetail objectForKey:@"ouname"]];
    
    NSString *title = [userDetail objectForKey:@"title"];
    NSString *mobile = [userDetail objectForKey:@"mobile"];
    NSString *teleoffice = [userDetail objectForKey:@"teleoffice"];
    NSString *telehome = [userDetail objectForKey:@"telehome"];
    NSString *mail = [userDetail objectForKey:@"mail"];
    
    mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    teleoffice = [teleoffice stringByReplacingOccurrencesOfString:@" " withString:@""];
    telehome = [telehome stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    
    if(title==nil || [title isEqual:@""]){
        [self.classLabel setText:@"职员"];
    }else{
        [self.classLabel setText:title];
    }
    
    if(mobile==nil || [mobile isEqual:@""]){
         [self.mobileLabel setText:@""];
        self.mobilecallButton.hidden = YES;
        self.mobilemsgButton.hidden = YES;
    }else{
         [self.mobileLabel setText:mobile];
    }
    
    if(teleoffice==nil || [teleoffice isEqual:@""]){
        [self.officePhoneLabel setText:@""];
        self.officecallButton.hidden = YES;
    }else{
        [self.officePhoneLabel setText:teleoffice];
    }
    
    if(telehome==nil || [telehome isEqual:@""]){
        [self.familyPhoneLabel setText:@""];
        self.familycallButton.hidden = YES;
    }else{
        [self.familyPhoneLabel setText:telehome];
    }
    
    if(mail==nil || [mail isEqual:@""]){
        [self.emailLabel setText:@""];
        self.emailButton.hidden = YES;
    }else{
        [self.emailLabel setText:mail];
    }
   
    [self.mobileTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.officeTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.familyTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    [self.emailTitleLabel setTextColor:UIColorFromRGB(0x0079fe)];
    
    
    
    [self getUserHeadImage];
    
    [self initHJManage];
    CGFloat ivLength = 100;
    if (CurrentDeviceISiPad) {
        ivLength = 150;
        headImgView = [[HJManagedImageV alloc]initWithFrame:CGRectMake(60,50,ivLength,ivLength)];
    }else{
        if (IS_IPHONE_5) {
            headImgView = [[HJManagedImageV alloc]initWithFrame:CGRectMake(20,42,ivLength,ivLength)];
        }else{
            headImgView = [[HJManagedImageV alloc]initWithFrame:CGRectMake(20,42-30,ivLength,ivLength)];
        }
    }
    
    
    [self.view addSubview:headImgView];
    
    if([self.sexLabel.text isEqual:@"男"]){
        [headImgView setImage:[UIImage imageNamed:@"img_man.png"]];
    }else{
        [headImgView setImage:[UIImage imageNamed:@"img_woman.png"]];
    }
    
    [headImgView.imageView setContentMode:UIViewContentModeScaleToFill];
    headImgView.layer.masksToBounds = YES;
    headImgView.layer.cornerRadius = ivLength/2;
    headImgView.layer.borderWidth = 2;
    [headImgView.layer setBorderColor:[[UIColor whiteColor] CGColor]];

}

#pragma mark 设置头像
-(void)setHeadImg{
    
    if(UserGuid!=nil && UserGuid.length>0){
        headImgView.url = [NSURL URLWithString:[EpointBizTools getUserHeadImageDataUrl:UserGuid]];
    }else{
        [headImgView setImage:[UIImage imageNamed:@"img_woman.png"]];
    }
    
    [objMan manage:headImgView];
    
}

//初始化图片缓存管理器
-(void)initHJManage
{
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:20 memCacheSize:30];
    NSString* cacheDirectory = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches/imgcache/flickr/"] ;
    HJMOFileCache* fileCache = [[HJMOFileCache alloc] initWithRootPath:cacheDirectory];
    objMan.fileCache = fileCache;
    fileCache.fileCountLimit = 100;
    fileCache.fileAgeLimit = 60*60*24*30; //1 week
    [fileCache trimCacheUsingBackgroundThread];
}


#pragma 获取用户头像
-(void)getUserHeadImage
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:[DBFrameService getUserGuid] forKey:[initParam objectForKey:@"userguid"]];
    [self startTask:@"EpointLoadUserHeadImageTask" withParams:params withTaskId:3];
}

-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    if (taskId==3) {
        [self setHeadImg];
    }
}

-(IBAction)clickMicroMsgButton:(id)sender{
    [self toastMessage:@"功能暂未开放，建设中..."];
}
-(IBAction)clickFavourButton:(id)sender{
    [self toastMessage:@"功能暂未开放，建设中..."];
}
-(IBAction)clickOaMailButton:(id)sender{
    MailInfo * mailDetail = [[MailInfo alloc]init];
    mailDetail.ToUserGuidList =[NSString stringWithFormat:@"%@;",UserGuid];
    mailDetail.ToUserDispNameList =[NSString stringWithFormat:@"%@;",[userDetail objectForKey:@"displayname"]];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:mailDetail forKey:@"MailInfo"];
    [params setObject:@"write" forKey:@"SendType"];
    EpointMailSendViewController *sendCtrl = [[EpointMailSendViewController alloc] initWithParams:params];
    [self.navigationController pushViewController:sendCtrl animated:YES];
}
-(IBAction)clickMobileMsgButton:(id)sender{
    
    [SGActionView showAlertWithTitle:@"确定发送短信吗？" message:nil leftButtonTitle:@"取消" rightButtonTitle:@"确定" selectedHandle:^(NSInteger index) {
        if (index == 1) {
            NSString *numFormat = [NSString stringWithFormat:@"sms://%@",self.mobileLabel.text];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
        }
    }];
}
-(IBAction)clickMobileCallButton:(id)sender{
    [SGActionView showAlertWithTitle:@"确定拨打电话吗？" message:nil leftButtonTitle:@"取消" rightButtonTitle:@"确定" selectedHandle:^(NSInteger index) {
        if (index == 1) {
            NSString *numFormat = [NSString stringWithFormat:@"tel://%@",self.mobileLabel.text];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
        }
    }];
    
}
-(IBAction)clickOfficeCallButton:(id)sender{
    [SGActionView showAlertWithTitle:@"确定拨打电话吗？" message:nil leftButtonTitle:@"取消" rightButtonTitle:@"确定" selectedHandle:^(NSInteger index) {
        if (index == 1) {
            NSString *numFormat = [NSString stringWithFormat:@"tel://%@",self.officecallButton.titleLabel.text];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];

        }
    }];

   }
-(IBAction)clickFamilyCallButton:(id)sender{
    [SGActionView showAlertWithTitle:@"确定拨打电话吗？" message:nil leftButtonTitle:@"取消" rightButtonTitle:@"确定" selectedHandle:^(NSInteger index) {
        if (index == 1) {
            NSString *numFormat = [NSString stringWithFormat:@"tel://%@",self.familycallButton.titleLabel.text];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
            
        }
    }];
   
}
-(IBAction)clickMailButton:(id)sender{
    if (CurrentDeviceISiPad) {
        RIButtonItem* cancelBtn = [RIButtonItem item];
        cancelBtn.label=@"取消";
        cancelBtn.action=^{};
        RIButtonItem* okBtn =[RIButtonItem item];
        okBtn.label=@"确定";
        okBtn.action=^{
             [self sendMail:self.emailLabel.text];
        };
        UIAlertView* sendMailAlert =[[UIAlertView alloc] initWithTitle:@"提示" message:@"确定发送邮件吗？" cancelButtonItem:cancelBtn otherButtonItems:okBtn, nil];
        [sendMailAlert show];
    }else{
        [SGActionView showAlertWithTitle:@"确定发送邮件吗？" message:nil leftButtonTitle:@"取消" rightButtonTitle:@"确定" selectedHandle:^(NSInteger index) {
            if (index == 1) {
                [self sendMail:self.emailLabel.text];
                
            }
        }];
    }  
}


-(void)sendMail:(NSString *)mail
{
    NSString *numFormat = [NSString stringWithFormat:@"mailto:%@",mail];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:numFormat]];
}



@end
